package com.example.fileserver_API.Controller.File;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.fileserver_API.Dao.SysappsettingMappers;
import com.example.fileserver_API.Entity.PO.sysappsettings;
import com.example.fileserver_API.Entity.PO.uploadpictures;
import com.example.fileserver_API.Entity.VO.APIResult;
import com.example.fileserver_API.FixedValue.SetSettings;
import com.example.fileserver_API.Interfaces.IService.IGetFileService;
import com.example.fileserver_API.utils.RedisUtils;
import com.example.fileserver_API.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import java.io.FileInputStream;
import java.io.InputStream;

@RestController
@RequestMapping("/API/GetFile")
@CrossOrigin
public class GetFileController {
    @Autowired
    IGetFileService _IGetFileService;
    @Autowired
    ResourceLoader resourceLoader;
    @Autowired
    RedisUtils redisUtils;

    /**
     * 获取图片文件流
     * @param guid 文件标识符
     * @return ResponseEntity<Resource>
     * @throws Exception 异常
     */
    @GetMapping("")
    public ResponseEntity<Resource> GetImg(String guid) throws Exception {
        try {
            uploadpictures uploadpictures = _IGetFileService.GetImg(guid);// 获取图片信息
            // 查询上传目录位置
            String realpath = redisUtils.GetHashValue(SetSettings.sys_syssetting,SetSettings.sys_path).toString();
            if (StringUtils.isBlank(realpath))
                throw new Exception("未查询到配置的[path]地址配置！");

            Resource resource = resourceLoader.getResource("file:" + realpath+uploadpictures.filepath);
            // 检查文件是否存在
            if (!resource.exists()) {
                return ResponseEntity.notFound().build();
            }
            // 设置响应头部信息
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.IMAGE_JPEG);
            // 返回文件流
            return ResponseEntity.ok()
                    .headers(headers)
                    .body(resource);
        } catch (Exception e) {
            // 处理异常情况
            return ResponseEntity.badRequest().build();
        }
    }

    /**
     * 下载文件
     * @param guid 文件唯一标识符
     * @return ResponseEntity<InputStreamResource>
     * @throws Exception 异常
     */
    @GetMapping("/downloadFile")
    public ResponseEntity<InputStreamResource> downloadFile(String guid) throws Exception {
        uploadpictures uploadpictures = _IGetFileService.GetImg(guid);// 获取图片信息
        // 查询上传目录位置
        String realpath = redisUtils.GetHashValue(SetSettings.sys_syssetting,SetSettings.sys_path).toString();
        if (StringUtils.isBlank(realpath))
            throw new Exception("未查询到配置的[path]地址配置！");

        String filePath = realpath+uploadpictures.filepath;
        InputStream inputStream = new FileInputStream(filePath);
        InputStreamResource resource = new InputStreamResource(inputStream);
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=download"+uploadpictures.getSuffix());
        return ResponseEntity
                .ok()
                .headers(headers)
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(resource);
    }

    /**
     * 删除某个文件
     * @param guid 文件guid
     * @return APIResult
     */
    @GetMapping("/DeleteFile")
    public APIResult DeleteFile(String guid){
        return _IGetFileService.DeleteFile(guid);
    }

    /**
     * 获取视频（不分片）
     * @param guid 图片guid
     * @return APIResult
     */
    @GetMapping("/GetVideo")
    public ResponseEntity<Resource> GetVideo(String guid){
        return _IGetFileService.GetVideo(guid);
    }
}
